// 还有倒计时
// 手机号码要验证
<template>
  <div class="login-index bg-fff">
    <div class="top">
      <nav class="top-nav">
        <div @click="$router.back()" class="back">
          <span class="iconfont icon-jiantou1"></span>
          <span class="text">返回</span>
        </div>
        <span class="title">登录</span>
        <span class="btn">&nbsp;&nbsp;</span>
      </nav>
    </div>

    <div class="pt-20 pl-30 pr-30 w100">
      <div class="tel w100 flex aic jc-sb">
        <input
          class="inp"
          type="tel"
          placeholder="请输入手机号码"
          :value="tel"
        />
        <span @click="getSmsCode">获取短信验证码</span>
      </div>
      <input
        type="text"
        class="yanzheng f999 flex aic"
        placeholder="请输入验证码"
        :value="smsCode"
      />

      <button class="login w100" @click="toLogin">短信登陆</button>
    </div>
  </div>
</template>

<script>
// 请求验证码

import { mapState } from "vuex";
import { get, post } from "@/request/index";
export default {
  data() {
    return {
      tel: "15975088150",
      smsCode: "",
    };
  },
  computed: {
    ...mapState(["phone"]),
  },
  methods: {
    getSmsCode() {
      post("/user/getSmsCode", { phone: this.tel })
        .then((res) => (this.smsCode = res.smsCode))
        .catch((err) => console.log("获取验证码失败", err));
      // (this.tel).then((res) => (this.smsCode = res));
    },
    toLogin() {
      get("/user/login", { phone: this.tel, smsCode: this.smsCode })
        .then((res) => {
          this.$store.commit("set_isLogin", true);
          this.$store.commit("set_token", res.result.token);
          this.$store.commit("set_phone", res.result.phone);
          console.log("成功登录", res);
          this.$router.back();
        })
        .catch((err) => console.log("登陆失败", err));
    },
  },
};
</script>

<style lang="less" scoped>
@import "./login.less";
</style>